<?php

namespace App\Http\Controllers;

use App\Models\Zone;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ZoneController extends Controller
{
    public function getList()
    {
        $lists = Zone::with('zone')->orderBy('id','asc')->paginate(10);
        return view("zone.index",[
            'actions'=>$this->actions,
            'lists'=>$this->lists
        ]);
    }
    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = Zone::with('zone')->where('id',$data['id'])->first();
        $pLists = Zone::where('level','0')->orWhere('level','1')->orderBy('id','desc')->get();
        return view('zone.edit')->with('list', $list)->with('pLists', $pLists);
    }
    public function postEdit(Request $request)
    {
        $data = $request->input();
        unset($data['_token'], $data['formType']);
        Zone::where('id',$data['id'])->update($data);
        return AjaxCallbackMessage('编辑成功',true,url('zone/list'));
    }
    public function getCreate()
    {
        $pLists = Zone::where('level', 0)->orWhere('level', 1)->orderBy('id', 'asc')->get();
        return view('zone.create')->with('pLists', $pLists);
    }
    public function postCreate(ZoneRequest $request)
    {
        $data = $request->input();
        unset($data['_token'], $data['formType']);
        $result = Zone::create($data);
        if (is_object($result)){
            return AjaxCallbackMessage('创建成功', true, url('zone/list'));
        }else {
            return AjaxCallbackMessage('创建失败，未知错误', false);
        }
    }
}
